﻿using _12._30._2014_MVC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _12._30._2014_MVC.Controllers
{
    public class HomeController : Controller
    {
        public HomeController()
        {
            
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.MyCoolProperty = "I just made this like JavaScript";
            ViewBag.Message = "Peanut Butter is soooooo good.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult Create()
        {
            CreateTodoViewModel model = new CreateTodoViewModel();
            
            List<TodoViewModel> allTodos = PopulateTodos();
            model.Todos = allTodos.Where(t => !t.IsCompleted).ToList();

            return View(model);
        }

        [HttpPost]
        public ActionResult Create(CreateTodoViewModel model)
        {
            model.Todos = PopulateTodos();

            model.Todos.Add(new TodoViewModel
            {
                DateCreated = DateTime.Now,
                Text = model.Text
            });

            return View(model);
        }

        public ActionResult Todos()
        {
            HomeViewModel model = new HomeViewModel();
            model.Todos = PopulateTodos();

            return View(model);
        }

        public ActionResult ViewCharacters()
        {
            StarWarsViewModel model = new StarWarsViewModel();

            model.Characters = new List<Character>
            {
                new Jedi {},
                new BountyHunter {},
                new BountyHunter {},
                new Jedi {}
            };

            return View(model);
        }

        #region Helpers Methods

        private static List<TodoViewModel> PopulateTodos()
        {
            List<TodoViewModel> model = new List<TodoViewModel>();

            model.Add(new TodoViewModel
            {
                DateCompleted = DateTime.Now.AddDays(-2),
                DateCreated = DateTime.Now.AddDays(-4),
                IsCompleted = false,
                Text = "Holiday"
            });
            model.Add(new TodoViewModel
            {
                DateCompleted = DateTime.Now.AddDays(-2),
                DateCreated = DateTime.Now.AddDays(-4),
                IsCompleted = true,
                Text = "Vacation"
            });
            model.Add(new TodoViewModel
            {
                DateCompleted = DateTime.Now.AddDays(-2),
                DateCreated = DateTime.Now.AddDays(-4),
                IsCompleted = true,
                Text = "Get Peanut Butter!"
            });
            model.Add(new TodoViewModel
            {
                DateCompleted = DateTime.Now.AddDays(-2),
                DateCreated = DateTime.Now.AddDays(-4),
                IsCompleted = false,
                Text = "Go to sleep"
            });

            return model;
        }

        #endregion
    }
}